#!/usr/bin/perl

use lib::abs qw( .. ../../base_lib );

use JIP::ToolSet;
use FindBin qw( $Bin );
use English qw( -no_match_vars );
use Test::More tests => 35;

BEGIN {
    use_ok 'Filesystem', qw(
        resolveCrawlerFileName
        parseCrawlerFileName
        getCrawlerOutputListing
        getFilterOutputListing
        resolveFilterOutputFileName
   );
}

require_ok 'Filesystem';

{
    try {
        resolveCrawlerFileName( 'tratata', 'tratata', 'tratata' )
    }
    catch {
        like(
            $ARG,
            qr/^you can also use named arguments/,
            'resolveCrawlerFileName() - with named parameters'
        )
    };

    try {
        resolveCrawlerFileName( named_parameter => 'tratata' )
    }
    catch {
        like(
            $ARG,
            qr/^required parameter "host_id" not defined at/,
            'resolveCrawlerFileName() - required parameter "host_id"'
        )
    };

    try {
        resolveCrawlerFileName( host_id => 69 )
    }
    catch {
        like(
            $ARG,
            qr/^required parameter "host_ip" not defined at/,
            'resolveCrawlerFileName() - required parameter "host_ip"'
        )
    };

    try {
        resolveCrawlerFileName( host_id => 6.9, host_ip => '10.0.14.140' )
    }
    catch {
        like(
            $ARG,
            qr/^"host_id" must be positive integer/,
            'resolveCrawlerFileName() - "host_id" must be positive integer'
        )
    };

    try {
        resolveCrawlerFileName( host_id => 69, host_ip => 'tratata' )
    }
    catch {
        like(
            $ARG,
            qr/^"host_ip" must be provided in IP-address format/,
            'resolveCrawlerFileName() - "host_ip" must be IP-address'
        )
    };

    try {
        is(
            resolveCrawlerFileName( host_id => 69, host_ip => '10.0.14.140' ),
            'ftp_69_10.0.14.140.raw.txt',
            'resolveCrawlerFileName() - is correct'
        )
    }
}

{
    my $resultSet = parseCrawlerFileName('/home/tmp/ftp_69_10.0.14.140.raw.txt');

    is( $resultSet->ID, 69, 'parseCrawlerFileName() - id-accessor');

    is(
        $resultSet->IPAddress,
        '10.0.14.140',
        'parseCrawlerFileName() - IPAddress-accessor'
    );

    try {
        parseCrawlerFileName('tratata')
    }
    catch {
        like(
            $ARG,
            qr/"host_id" not defined/,
            'parseCrawlerFileName() - incorrect file name'
        )
    };
}

{
    try {
        getCrawlerOutputListing('/tratata')
    }
    catch {
        like(
            $ARG,
            qr/^directory \(\/tratata\) not found/,
            'getCrawlerOutputListing() - incorrect path'
        )
    };
}

{
    try {
        getCrawlerOutputListing('./scratch/crawler/');

        pass 'getCrawlerOutputListing() accepts relative path to directiry';
    }
    catch {
        fail 'getCrawlerOutputListing() with error: '. $ARG;
    };

    my @resultSet = getCrawlerOutputListing('./scratch/crawler/');

    is( scalar( @resultSet ), 1, 'getCrawlerOutputListing() - returned array' );

    is(
        $resultSet[0]->name,
        'ftp_67_217.197.0.98.raw.txt',
        'getCrawlerOutputListing() - name accessor'
    );

    is(
        $resultSet[0]->relativePath,
        'scratch/crawler/ftp_67_217.197.0.98.raw.txt',
        'getCrawlerOutputListing() - relativePath accessor'
    );

    is(
        $resultSet[0]->absolutePath,
        $Bin .'/scratch/crawler/ftp_67_217.197.0.98.raw.txt',
        'getCrawlerOutputListing() - absolutePath accessor'
    );

    try {
        my @absRS = getCrawlerOutputListing( $Bin .'/scratch/crawler/' );

        pass 'getCrawlerOutputListing() accepts absolute path to directiry';

        is(
            $absRS[0]->name,
            $resultSet[0]->name,
            'getCrawlerOutputListing() - absolute name equiv relative name'
        );

        is(
            $absRS[0]->relativePath,
            $resultSet[0]->relativePath,
            'getCrawlerOutputListing() - relative path equiv relative path'
        );

        is(
            $absRS[0]->absolutePath,
            $resultSet[0]->absolutePath,
            'getCrawlerOutputListing() - absolute path equiv absolute path'
        );
    }
    catch {
        fail 'getCrawlerOutputListing() with error: '. $ARG;
    };
}

{
    try {
        getFilterOutputListing('/tratata')
    }
    catch {
        like(
            $ARG,
            qr/^directory \(\/tratata\) not found/,
            'getFilterOutputListing() - incorrect path'
        )
    };
}

{
    try {
        getFilterOutputListing('./scratch/filter/');

        pass 'getFilterOutputListing() accepts relative path to directiry';
    }
    catch {
        fail 'getFilterOutputListing() with error: '. $ARG;
    };

    my @resultSet = getFilterOutputListing('./scratch/filter/');

    is( scalar( @resultSet ), 1, 'getFilterOutputListing() - returned array' );

    is(
        $resultSet[0]->name,
        'ftp_video_127.0.0.1.filter.txt',
        'getFilterOutputListing() - name accessor'
    );

    is(
        $resultSet[0]->relativePath,
        'scratch/filter/ftp_video_127.0.0.1.filter.txt',
        'getFilterOutputListing() - relativePath accessor'
    );

    is(
        $resultSet[0]->absolutePath,
        $Bin .'/scratch/filter/ftp_video_127.0.0.1.filter.txt',
        'getFilterOutputListing() - absolutePath accessor'
    );

    try {
        my @absRS = getFilterOutputListing( $Bin .'/scratch/filter/' );

        pass 'getFilterOutputListing() accepts absolute path to directiry';

        is(
            $absRS[0]->name,
            $resultSet[0]->name,
            'getFilterOutputListing() - absolute name equiv relative name'
        );

        is(
            $absRS[0]->relativePath,
            $resultSet[0]->relativePath,
            'getFilterOutputListing() - relative path equiv relative path'
        );

        is(
            $absRS[0]->absolutePath,
            $resultSet[0]->absolutePath,
            'getFilterOutputListing() - absolute path equiv absolute path'
        );
    }
    catch {
        fail 'getFilterOutputListing() with error: '. $ARG;
    };
}

{
    try {
        resolveFilterOutputFileName( 'tratata', 'tratata', 'tratata' )
    }
    catch {
        like(
            $ARG,
            qr/^you can also use named arguments/,
            'resolveFilterOutputFileName() - with named parameters'
        )
    };

    try {
        resolveFilterOutputFileName( named_parameter => 'tratata')
    }
    catch {
        like(
            $ARG,
            qr/^required parameter "ip_address" not defined at/,
            'resolveFilterOutputFileName() - required parameter "ip_address"'
        )
    };

    try {
        resolveFilterOutputFileName( ip_address => '127.0.0.1' )
    }
    catch {
        like(
            $ARG,
            qr/^required parameter "content_type" not defined at/,
            'resolveFilterOutputFileName() - required parameter "content_type"'
        )
    };

    try {
        like(
            resolveFilterOutputFileName(
                ip_address => '127.0.0.1',
                content_type => 'video'
            ),
            qr/ftp_video_127.0.0.1.filter.txt/,
            'resolveFilterOutputFileName() - is correct'
        )
    }
}
